package com.learner.common.exception;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public enum RestStatus implements IRestStatus {

    /**
     * 成功
     */
    SUCCESS(0),
    ERROR(-1),
    UNAUTHORIZED(-HttpServletResponse.SC_UNAUTHORIZED, "未授权访问"),
    TOKEN_INVALID(-HttpServletResponse.SC_UNAUTHORIZED, "Token无效"),
    FORBIDDEN(-HttpServletResponse.SC_FORBIDDEN, "禁止访问"),
    NOT_FOUND(-HttpServletResponse.SC_NOT_FOUND, "内容不存在"),
    NOT_MODIFIED(-HttpServletResponse.SC_NOT_MODIFIED, "内容未变更"),
    SERVER_ERROR(-HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器异常");

    public Integer code;
    public String msg;

    RestStatus(Integer code){
        this.code = code;
        switch (code){
            case 0 : this.msg = "执行成功" ;break;
            case -1 : this.msg = "系统繁忙，请稍后再试" ;break;
            default: this.msg = "系统未定义";
        }
    }

    RestStatus(Integer code, String msg){
        this.code = code;
        this.msg = msg;
    }


    @Override
    public int code() {
        return code;
    }

    @Override
    public String message() {
        return msg;
    }

    public Map<String, Object> toMap() {
        Map map = new HashMap<String, Object>();
        map.put("code", this.code);
        map.put("msg", this.msg);
        return map;
    }

    @Override
    public String toString(){
        return JSON.toJSONString(this.toMap());
    }

}
